{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "932b13d4-bdef-43e9-ad5e-88199b3ab5a9",
   "metadata": {},
   "source": [
    "# 统计"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "701ed797-3515-4277-8712-86826cf94ff1",
   "metadata": {},
   "source": [
    "常用统计函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3c4a0b77-0a3f-4bb2-9f73-99050c31ef55",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f1cb9c36-8b5c-45b9-a405-a1a776d58901",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.read_csv(r\"./out/data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "e7a4aece-da0e-4180-a7b6-4072cd873f47",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Key Developments By Date     object\n",
       "Key Developments by Type     object\n",
       "Company Name(s)              object\n",
       "Key Development Headline     object\n",
       "Key Development Situation    object\n",
       "Key Development Sources      object\n",
       "Geographic Locations         object\n",
       "dtype: object"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "98935910-9053-4dd8-86e4-cc63c2214ec6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = df.convert_dtypes()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "1c46226a-987a-4428-9a70-4f3d34e0e97a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Key Developments By Date     string[python]\n",
       "Key Developments by Type     string[python]\n",
       "Company Name(s)              string[python]\n",
       "Key Development Headline     string[python]\n",
       "Key Development Situation    string[python]\n",
       "Key Development Sources      string[python]\n",
       "Geographic Locations         string[python]\n",
       "dtype: object"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "f1188fa8-646b-40e6-a88c-57d6b8098d2a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df[\"Key Developments By Date\"] = pd.to_datetime(df[\"Key Developments By Date\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "5cd5230a-2e20-4e72-860a-1f51a4cafc17",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Key Developments By Date     datetime64[ns]\n",
       "Key Developments by Type     string[python]\n",
       "Company Name(s)              string[python]\n",
       "Key Development Headline     string[python]\n",
       "Key Development Situation    string[python]\n",
       "Key Development Sources      string[python]\n",
       "Geographic Locations         string[python]\n",
       "dtype: object"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "cf4b9a0c-2053-4702-8e7b-d7828787c03d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Key Developments By Date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2022-05-31 22:48:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>2021-03-24 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2021-09-27 18:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2022-06-17 12:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2023-01-18 00:00:00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2023-12-01 00:00:00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Key Developments By Date\n",
       "count                       20\n",
       "mean       2022-05-31 22:48:00\n",
       "min        2021-03-24 00:00:00\n",
       "25%        2021-09-27 18:00:00\n",
       "50%        2022-06-17 12:00:00\n",
       "75%        2023-01-18 00:00:00\n",
       "max        2023-12-01 00:00:00"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.describe()  # 查看数据值列的汇总统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "afa6cc93-b025-4e3a-acd0-0e5bd7100996",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df1 = pd.DataFrame({'A': [1, 1, 2, 2, 3, 3],\n",
    "                   'B': [2, 3, 1, 2, 1, 3]})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "4549f99a-1a98-493d-a7de-6fbfc6a82f92",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2.0\n",
       "B    2.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.mean()  # 返回所有列的均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "07e81770-4406-4684-ae12-48ad64ef87ad",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1.00</td>\n",
       "      <td>-0.25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-0.25</td>\n",
       "      <td>1.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      A     B\n",
       "A  1.00 -0.25\n",
       "B -0.25  1.00"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.corr()  # 返回列与列之间的相关系数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6e5b3fb5-30ad-4592-b1c5-a033fb03adcb",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    6\n",
       "B    6\n",
       "dtype: int64"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.count()  # 返回每一列中的非空值的个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "0f7c8f3a-de69-4d94-b31a-83519c78e63f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    3\n",
       "B    3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.max()  # 返回每一列的最大值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "29bf44a4-f90f-461d-b9a9-d412cc981e12",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    1\n",
       "B    1\n",
       "dtype: int64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.min()  # 返回每一列的最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "1dc8022c-1f9c-46e3-a249-c5dc0b12108a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    2.0\n",
       "B    2.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.median()  # 返回每一列的中位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "884b9a18-ca5b-4e57-a424-a70a93ba5268",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "A    0.894427\n",
       "B    0.894427\n",
       "dtype: float64"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1.std()  # 返回每一列的标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcf89ed7-2222-4546-afe2-8eed68da6bdd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
